package com.lineage.server.clientpackets;

import com.lineage.echo.ClientExecutor;
import com.lineage.server.datatables.ItemTable;
import com.lineage.server.datatables.lock.CastleReading;
import com.lineage.server.model.Instance.L1ItemInstance;
import com.lineage.server.model.Instance.L1PcInstance;
import com.lineage.server.model.L1Clan;
import com.lineage.server.serverpackets.S_ServerMessage;
import com.lineage.server.serverpackets.ServerBasePacket;
import com.lineage.server.templates.L1Castle;
import com.lineage.server.world.WorldClan;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class C_Drawal extends ClientBasePacket {
  private static final Log _log = LogFactory.getLog(C_Drawal.class);
  
  public void start(byte[] decrypt, ClientExecutor client) {
    try {
      read(decrypt);
      L1PcInstance pc = client.getActiveChar();
      if (!pc.isGhost() && 
        !pc.isDead() && 
        !pc.isTeleport()) {
        int objid = readD();
        long count = readD();
        if (count > 2147483647L)
          count = 2147483647L; 
        count = Math.max(0L, count);
        L1Clan clan = WorldClan.get().getClan(pc.getClanname());
        if (clan == null)
          return; 
        int castle_id = clan.getCastleId();
        if (castle_id == 0)
          return; 
        if (!pc.isCrown()) {
          pc.sendPackets((ServerBasePacket)new S_ServerMessage(518));
          return;
        } 
        if (pc.getId() != clan.getLeaderId())
          return; 
        L1Castle l1castle = CastleReading.get().getCastleTable(castle_id);
        L1ItemInstance item = ItemTable.get().createItem(40308);
        long money = l1castle.getPublicMoney() - count;
        if (item != null && money >= 0L) {
          l1castle.setPublicMoney(money);
          CastleReading.get().updateCastle(l1castle);
          if (pc.getInventory().checkAddItem(item, count) == 0)
            pc.getInventory().storeItem(40308, count); 
          pc.sendPackets((ServerBasePacket)new S_ServerMessage(143, "$457", "$4 (" + count + ")"));
          return;
        } 
        return;
      } 
      return;
    } catch (Exception e) {
      _log.error(e.getLocalizedMessage(), e);
    } finally {
      over();
    } 
  }
  
  public String getType() {
    return getClass().getSimpleName();
  }
}
